package org.huqiwen.blog.logic;
/**
 * 此类用来分割字符串，但是很不完善，现在是后半部分基本没有用着，只是用了前半部分也就是StripHTML为True的
 * 情况，这种情况是不保留HTML格式，为False时是在截取摘要时保留HTML格式，但是现在的这个类写的还不完善
 * @author Administrator
 *
 */
public class SubStringTest {
	public  String GetContentSummary(String content, int length, Boolean StripHTML)
    {
        if (content.length()==0 || length == 0)
            return "";
        if (StripHTML)
        {
//            Pattern re = Pattern.compile("<[^>]*>");
//            Matcher m = re.matcher(content);
//          
            content = content.replaceAll("<[^>]*>", "");
            content = content.replaceAll("　", "").replaceAll(" ", "");
            if (content.length() <= length)
                return content;
            else
                return content.substring(0, length) + "……";
        }
        else
        {
            if (content.length() <= length)
                return content;

            int pos = 0, npos = 0, size = 0;
            Boolean firststop = false, notr = false, noli = false;
            StringBuilder sb = new StringBuilder();
            while (true)
            {
                if (pos >= content.length()) break;
                String cur = content.substring(pos, pos+1);             
                if (cur == "<")
                {
                    String next = content.substring(pos + 1, pos+3).toLowerCase();
                    if (next.indexOf("p") == 0 && next.indexOf("pre") != 0)
                    {
                        npos = content.indexOf(">", pos) + 1;
                    }
                    else if (next.indexOf("/p") == 0 && next.indexOf("/pr") != 0)
                    {
                        npos = content.indexOf(">", pos) + 1;
                        if (size < length)
                            sb.append("<br/>");
                    }
                    else if (next.indexOf("br") == 0)
                    {
                        npos = content.indexOf(">", pos) + 1;
                        if (size < length)
                            sb.append("<br/>");
                    }
                    else if (next.indexOf("img") == 0)
                    {
                        npos = content.indexOf(">", pos) + 1;
                        if (size < length)
                        {
                            sb.append(content.substring(pos, npos - pos));
                            size += npos - pos + 1;
                        }
                    }
                    else if (next.indexOf("li") == 0||next.indexOf("/li") == 0)
                    {
                        npos = content.indexOf(">", pos) + 1;
                        if (size < length)
                        {
                            sb.append(content.substring(pos, npos - pos));
                        }
                        else
                        {
                            if (!noli && next.indexOf("/li") == 0)
                            {
                                sb.append(content.substring(pos, npos - pos));
                                noli = true;
                            }
                        }
                    }
                    else if (next.indexOf("tr") == 0 || next.indexOf("/tr") == 0)
                    {
                        npos = content.indexOf(">", pos) + 1;
                        if (size < length)
                        {
                            sb.append(content.substring(pos, npos - pos));
                        }
                        else
                        {
                            if (!notr && next.indexOf("/tr") == 0)
                            {
                                sb.append(content.substring(pos, npos - pos));
                                notr = true;
                            }
                        }
                    }
                    else if (next.indexOf("td") == 0 || next.indexOf("/td") == 0)
                    {
                        npos = content.indexOf(">", pos) + 1;
                        if (size < length)
                        {
                            sb.append(content.substring(pos, npos - pos));
                        }
                        else
                        {
                            if (!notr)
                            {
                                sb.append(content.substring(pos, npos - pos));
                            }
                        }
                    }
                    else
                    {
                        npos = content.indexOf(">", pos) + 1;
                        sb.append(content.substring(pos, npos - pos));
                    }
                    if (npos <= pos)
                        npos = pos+1;
                    pos = npos;
                }
                else
                {
                    if (size < length)
                    {
                        sb.append(cur);
                        size++;
                    }
                    else
                    {
                        if (!firststop)
                        {
                            sb.append("……");
                            firststop = true;
                        }
                    }
                    pos++;
                }

            }
           return sb.toString();
        }
    }	
}
